﻿using System;
using Amazon.SimpleDB;

namespace SimpleDbBrowser
{
    /// <summary>
    /// The configuration information required for accessing the SimpleDb
    /// </summary>
    public sealed class AwsConfiguration
    {
        /// <summary>
        /// Initializes a new instance of the AwsConfiguration class.
        /// </summary>
        /// <param name="accessKey">
        /// The SimpleDB Access Key.
        /// </param>
        /// <param name="secretKey">
        /// The SimpleDB secret key.
        /// </param>
        /// <param name="manualProxyConfig">
        /// True to indicate that the proxy has been configured manually.
        /// </param>
        /// <param name="proxyHost">
        /// The proxy host.
        /// </param>
        /// <param name="proxyPort">
        /// The proxy port.
        /// </param>
        public AwsConfiguration(string accessKey, string secretKey, bool manualProxyConfig, string proxyHost, int proxyPort)
        {
            if (accessKey == null) throw new ArgumentNullException("accessKey");
            if (secretKey == null) throw new ArgumentNullException("secretKey");
            if (manualProxyConfig && proxyHost == null) throw new ArgumentNullException("proxyHost");

            this.AccessKey = accessKey;
            this.SecretKey = secretKey;
            this.ManualProxyConfig = manualProxyConfig;
            this.ProxyHost = proxyHost;
            this.ProxyPort = proxyPort;
        }

        /// <summary>
        /// Gets the AwsConfiguration's access key.
        /// </summary>
        public string AccessKey { get; private set; }

        /// <summary>
        /// Gets the AwsConfigurations secret key.
        /// </summary>
        public string SecretKey { get; private set; }

        /// <summary>
        /// Gets a value indicating whether the proxy has been manually configured.
        /// </summary>
        public bool ManualProxyConfig { get; private set; }

        /// <summary>
        /// Gets the proxy host that must be used for manual proxy configuration.
        /// </summary>
        public string ProxyHost { get; private set; }

        /// <summary>
        /// Gets the port that the manual proxy configuration has been set to.
        /// </summary>
        public int ProxyPort { get; private set; }

        /// <summary>
        /// Gets a value indicating whether or not the configuration may be validns.
        /// </summary>
        /// <remarks>
        /// This doesn't guarantee that the configuration will successfully 
        /// connect to the AWS simpledb.  It only means that it _may_.
        /// </remarks>
        public bool IsValid
        {
            get
            {
                return !(string.IsNullOrWhiteSpace(this.SecretKey) || string.IsNullOrWhiteSpace(this.AccessKey));;
            }
        }

        /// <summary>
        /// Gets an AmazonSimpleDBConfig that reflects options set in this config.
        /// </summary>
        public AmazonSimpleDBConfig SimpleDbConfig
        {
            get
            {
                return this.ManualProxyConfig ? new AmazonSimpleDBConfig { ProxyHost = ProxyHost, ProxyPort = ProxyPort } : new AmazonSimpleDBConfig();
            }
        }
    }
}